package ai.fabu.dataplatform.controller;

import ai.fabu.dataplatform.bean.front.GetFrontData;
import ai.fabu.dataplatform.bean.task.GroupDataTask;
import ai.fabu.dataplatform.help.Result;
import ai.fabu.dataplatform.service.ApiForFrontService;
import ai.fabu.dataplatform.service.ApiForImportBagService;
import ai.fabu.dataplatform.service.ApiForTaskService;
import ai.fabu.dataplatform.vo.groupdata.GroupDataGetVO;
import ai.fabu.dataplatform.vo.groupdata.GroupDataQueryVO;
import ai.fabu.dataplatform.vo.metadata.UpdateMetadataVO;
import ai.fabu.dataplatform.vo.staticmap.GenerateStaticMapVO;
import ai.fabu.dataplatform.vo.task.GroupDataTaskImportVO;
import ai.fabu.dataplatform.vo.task.GroupDataTaskResetVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static ai.fabu.dataplatform.constant.ConstantName.MAP_NAME;
import static ai.fabu.dataplatform.constant.ConstantName.VEHICLE_NAME;
import static ai.fabu.dataplatform.constant.ConstantValue.MAP_TYPE;
import static ai.fabu.dataplatform.constant.ConstantValue.VEHICLE_TYPE;

@Slf4j
@RestController
@RequestMapping("/api")
public class ApiController {
    @Autowired
    private ApiForFrontService apiForFrontService;
    @Autowired
    ApiForTaskService apiForTaskService;


    /**
     * 通过给定 group id 或者 group name 来获取指定的 group
     * @param groupDataQueryVO
     * @return
     */

    @PostMapping(value = "/queryGroup", produces = "application/json")
    public Result queryGroup(@RequestBody GroupDataQueryVO groupDataQueryVO) {
        log.info("queryGroup: start query group data!");
        Result<Map<String, Object>> result = apiForFrontService.queryGroup(groupDataQueryVO);
        log.info("queryGroup: end query group data!");
        return result;
    }




    /**
     * 根据给定的条件查询所有满足条件的 group 条件主要是地图id，车辆id，起始时间，终止时间
     * @param groupDataGetVO
     * @return
     */
    @PostMapping(value = "/getData", produces = "application/json")
    public Result getData(@RequestBody GroupDataGetVO groupDataGetVO) {
        log.info("getData: start list group now!");
        Result<List<GetFrontData>> result = apiForFrontService.getDataByKeyList(groupDataGetVO);
        log.info("getData: end list group!");
        return result;
    }

    /**
     * 导包操作，条件是 path, metadata, map name, vehicle name, begin time, end time，然后把信息存储到一个 task 工作表中
     * @param groupDataTaskImportVO
     * @return
     */
    //用这个 改名即可
    @PostMapping("/import")
    public Result importGroupAsTask(@RequestBody GroupDataTaskImportVO groupDataTaskImportVO) {
        log.info("importGroupAsTask: start import group as task!");
        Result result = apiForFrontService.importGroupAsTask(groupDataTaskImportVO);
        log.info("importGroupAsTask: end import group as task!");
        return result;
    }

    /**
     * 给定起始时间和结束时间，查询在这段时间内的所有导入任务
     * @param beginTime
     * @param endTime
     * @return
     */

    @GetMapping("/monitor")
    public Result monitor(@RequestParam(value = "begin_time", defaultValue = "1000000000000") Long beginTime,
                          @RequestParam(value = "end_time", defaultValue = "9999999999999") Long endTime) {
        log.info("monitor: start query task!");
        Result<List<GroupDataTask>> result = apiForFrontService.monitor(beginTime, endTime);
        log.info("monitor: task number: {}", result.getData().size());
        log.info("monitor: end query task!");
        return result;
    }

    /**
     * for connecting test
     * @return
     */

    @GetMapping("/home1")
    public Result helloWorld1() {
        log.info("helloWorld1: start hello world!");
        Result<Long> result = apiForFrontService.temporaryFunction1();
        log.info("helloWorld1: end hello world!");
        return result;
    }

    @GetMapping("/home2")
    public Result helloWorld2() {
        log.info("helloWorld2: start hello world!");
        Result<Long> result = apiForFrontService.temporaryFunction2();
        log.info("helloWorld2: end hello world!");
        return result;
    }

    @GetMapping("/home3")
    public Result helloWorld3() {
        log.info("helloWorld3: start hello world!");
        Result<Long> result = apiForFrontService.temporaryFunction3();
        log.info("helloWorld3: end hello world!");
        return result;
    }


    /**
     * 返回所有的 map name
     * @return
     */

    @GetMapping("/listMapName")
    public Result listMapNameAll() {
        log.info("listMapNameAll: start query all map name!");
        Result<List<String>> result = apiForFrontService.getMapNameOrVehicleNameAll(MAP_NAME, MAP_TYPE);
        log.info("listMapNameAll: end query all map name!");
        return result;
    }

    /**
     * 返回所有的 vehicle name
     * @return
     */

    @GetMapping("/listVehicleName")
    public Result listVehicleNameAll() {
        log.info("listVehicleName: start query all vehicle name!");
        Result<List<String>> result = apiForFrontService.getMapNameOrVehicleNameAll(VEHICLE_NAME, VEHICLE_TYPE);
        log.info("listVehicleName: end query all vehicle name!");
        return result;
    }

    /**
     * 给定一个 group path 读取 该目录下的 README 文件，并且进行格式化
     * @param groupPath
     * @return
     */

    @GetMapping("/getGroupReadMe")
    public Result getGroupReadMeByGroupPath(@RequestParam(value = "group_path", defaultValue = "") String groupPath){
        log.info("getGroupReadMeByGroupPath: start get group README!");
        Result<String> result = apiForFrontService.readGroupREADME(groupPath);
        log.info("getGroupReadMeByGroupPath: end get group README!");
        return result;
    }

    @PostMapping("/resetGroupTask")
    public Result resetGroupDataTask(@RequestBody GroupDataTaskResetVO groupDataTaskResetVO){
        log.info("resetGroupDataTask: start reset group task!");
        Result result = apiForFrontService.resetGroupDataTask(groupDataTaskResetVO);
        log.info("resetGroupDataTask: end reset group task!");
        return result;
    }

    @PostMapping("/updateGroupMetadata")
    public Result updateGroupMetadata(@RequestBody UpdateMetadataVO updateMetadataVO){
        log.info("updateGroupMetadata: start update group metadata");
        Result result = apiForFrontService.updateGroupMetadata(updateMetadataVO);
        log.info("updateGroupMetadata: end update group metadata");
        return result;
    }


    @GetMapping("/pointCloudImage/{group_name}/{bag_name}/{topic_name}/{frame_no}")
    public Result<byte[]> getPointCloud(@PathVariable(value = "group_name") String groupName, @PathVariable(value = "bag_name") String bagName,
                                @PathVariable(value = "topic_name") String topicName, @PathVariable(value = "frame_no") Integer frameNo){

        log.info("getPointCloud: start get point cloud image");
        topicName = topicName.replace('-', '/');
        Result<byte[]> result = apiForFrontService.getPointCloudImage(groupName, bagName, topicName, frameNo);
        log.info("getPointCloud: end get point cloud image");
        return result;
    }


}
